// pages/orderCenter/index.js

import requestUtil from '../../baseRequest'
import requestUrls from '../../requestUrl'
import {
  $Message
} from '../../components/base/index'
import commonUtils from '../../utils/util.js'

let openid = getApp().getUserOpenid()
let DEFAULT_PAGE = 1
let DEFAULT_SIZE = 10

Page({

  /**
   * 页面的初始数据
   */
  data: {
    openId: openid,
    queryParam: {
      statusId: '20',
      searchType: 'hydt'
    },
    pagination: {
      pageSize: 5,
      pageNum: 1,
      total: 0
    },
    customItem: '全部',
    fromRegion: [],
    toRegion: [],
    orderCenterList: [],
    beginDate: '',
    endDate: '',
    showSearchMore: false,
    carLengthList: [
      { label: '不限车长', value: 'all' },
      { label: '1.8米', value: '1.8' },
      { label: '2.7米', value: '2.7' },
      { label: '3.8米', value: '3.8' },
      { label: '4.2米', value: '4.2' },
      { label: '5米', value: '5' },
      { label: '6.2米', value: '6.2' },
      { label: '6.8米', value: '6.8' },
      { label: '7.7米', value: '7.7' },
      { label: '8.2米', value: '8.2' },
      { label: '8.7米', value: '8.7' },
      { label: '9.6米', value: '9.6' },
      { label: '11.7米', value: '11.7' },
      { label: '12.5米', value: '12.5' },
      { label: '13米', value: '13' },
      { label: '13.7米', value: '13.7' },
      { label: '15米', value: '15' },
      { label: '16米', value: '16' },
      { label: '17.5米', value: '17.5' },
    ],
    carTypeList: [
      '不限车型', '平板', '高栏', '厢式', '集装箱', '面包车',
      '棉被车', '爬梯车', '高低板', '飞翼车'
    ],
    TimeList: ['不限时间', '今天', '明天', '后天'],
    carLength: 'all',
    carType: '不限车型',
    searchTime: '不限时间',
  },
  //修改出发地信息
  fromRegionChange: function (e) {
    const regionList = [...e.detail.code, "", "", ""];
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        fromProvinceId: regionList[0],
        fromCityId: regionList[1],
        fromCountyId: regionList[2],
      },
      fromRegion: e.detail.value,
    });
    this.handleSearch();
  },
  //修改目的地信息
  toRegionChange: function (e) {
    const regionList = [...e.detail.code, "", "", ""];
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        toProvinceId: regionList[0],
        toCityId: regionList[1],
        toCountyId: regionList[2],
      },
      toRegion: e.detail.value,
    });
    this.handleSearch();
  },
  //点击货源信息，跳转到货源页面
  showDetail: function (event) {
    const id = event.currentTarget.dataset.ordercenterid;
    wx.navigateTo({
      url: '/pages/orderCenter/detail?orderCenterId=' + id + '&publishSide=TRUCK_CENTER'
    })

  },
  //修改车辆类型
  bindTruckTypeChange: function (event) {
    const truckTypeValue = event.detail.value;
    this.setData({
      truckType: this.data.truckTypeList[truckTypeValue],
      queryParam: {
        ...this.data.queryParam,
        truckType: this.data.truckTypeList[truckTypeValue],
      },
      orderCenterList: [],
    })
  },
  //修改起始时间
  bindBeginDateChange: function (event) {
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        loadingBeginTime: event.detail.value,
      },
      beginDate: event.detail.value,
    })
  },
  //修改截止时间
  bindEndDateChange: function (event) {
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        loadingEndTime: event.detail.value,
      },
      endDate: event.detail.value,
    })
  },
  bindVolumnChange: function (event) {
    const volumn = event.detail.value;
    if (Number(volumn) < 0 || !commonUtils.checkDouble(volumn)) {
      this.showErrorMessage("体积数值不符合要求");
      this.setData({
        goodsVolume: 0
      })
    } else {
      this.setData({
        queryParam: {
          ...this.data.queryParam,
          goodsVolume: volumn,
        },
        goodsVolume: volumn,
      })
    }
  },
  bindWeightChange: function (event) {
    const weight = event.detail.value;
    if (Number(weight) < 0 || !commonUtils.checkDouble(weight)) {
      this.showErrorMessage("重量数值不符合要求");
      this.setData({
        goodsWeight: ''
      })
    } else {
      this.setData({
        queryParam: {
          ...this.data.queryParam,
          goodsWeight: weight,
        },
        goodsWeight: weight,
      })
    }
  },
  //点击搜索按钮，搜索货源
  searchOrderCenterList: function (event) {
    const formData = {
      openId: this.data.openId,
      criteria: {
        ...this.data.queryParam,
      },
      pageDto: {
        ...this.data.pagination
      }
    }
    requestUtil.postRequest(requestUrls.orderCenter.getPage, formData, (res) => {
      if (!res.hasError) {
        const { list = [] } = res;
        let orderCenterList = [];
        if (list.length > 0) {
          list.forEach(item => {
            orderCenterList.push({
              ...item,
              loadingBeginTime: commonUtils.formatDate(item.loadingBeginTime, 'MM-dd hh'),
              loadingEndTime: commonUtils.formatDate(item.loadingEndTime, 'MM-dd hh'),
            })
          })
        }
        this.setData({
          orderCenterList: this.data.orderCenterList.concat(orderCenterList),
          showSearchMore: false,
          pagination: {
            ...this.data.pagination,
            total: res.total,
          },
        })
      }
    })
  },
  makePhoneCall(event) {
    const phoneNumber = event.currentTarget.dataset.phonenumber;
    commonUtils.callSomeBody(phoneNumber);
  },
  changeCarLength(event) {
    const carLength = event.currentTarget.dataset.carlength;
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        requireTruckLength: carLength,
      },
      carLength: carLength,
      lengthInput: ''
    })
  },
  carLengthInputChange(event) {
    const lengthInput = event.detail.value;
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        requireTruckLength: lengthInput,
      },
      carLength: lengthInput,
      lengthInput: lengthInput
    })
  },
  changeCarType(event) {
    const carType = event.currentTarget.dataset.cartype;
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        requireTruckTypeName: carType,
      },
      carType: carType,
    })
  },
  //关闭筛选条件页面
  closeSearchDiv() {
    this.setData({
      orderCenterList: [],
      showSearchMore: false,
      showSearchTime: false,
    });
    this.searchOrderCenterList();
  },
  changeTime(event) {
    const searchTime = event.currentTarget.dataset.searchtime;
    if(searchTime !=='不限时间'){
      const date = new Date();
      const year = date.getFullYear();
      const month = date.getMonth();
      const day = date.getDate();
      let loadingBeginTime ='';
      let loadingEndTime ='';
      if(searchTime==='今天'){
        loadingBeginTime = new Date(year,month,day,0,0,0);
        loadingEndTime = new Date(year,month,day,23,59,59);
      }else if(searchTime==='明天'){
        loadingBeginTime = new Date(year,month,day+1,0,0,0);
        loadingEndTime = new Date(year,month,day+1,23,59,59);
      }else if(searchTime==='后天'){
        loadingBeginTime = new Date(year,month,day+2,0,0,0);
        loadingEndTime = new Date(year,month,day+2,23,59,59);
      }
      this.setData({
        queryParam: {
          ...this.data.queryParam,
          loadingBeginTime:commonUtils.formatDate(loadingBeginTime),
          loadingEndTime:commonUtils.formatDate(loadingEndTime),
        },
        searchTime:searchTime,
      })
    }else{
      this.setData({
        searchTime:searchTime,
        queryParam: {
          ...this.data.queryParam,
          loadingBeginTime:'',
          loadingEndTime:'',
        },
      })
    }
  },
  /**显示错误信息 */
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },
  showMoreSearch() {
    this.setData({
      orderCenterList: [],
      showSearchMore: true,
    })
  },
  showSearchTime() {
    this.setData({
      showSearchMore: false,
      showSearchTime: true,
    })
  },
  closeMoreSearch() {
    this.setData({
      showSearchMore: false,
      showSearchTime: false,
    })
  },
  handleSearch() {
    this.setData({
      orderCenterList: [],
      pagination: {
        ...this.data.pagination,
        pageNum: 1,
      },
    });
    this.searchOrderCenterList();
  },
  clearFromRegion() {
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        fromProvinceId: '',
        fromCityId: '',
        fromCountyId: ''
      },
      fromRegion: [],
    })
  },
  clearToRegion() {
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        toProvinceId: '',
        toCityId: '',
        toCountyId: ''
      },
      toRegion: [],
    })
  },
  clearBeginDate() {
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        freeBeginTime: '',
      },
      beginDate: '',
    })
  },
  clearEndDate() {
    this.setData({
      queryParam: {
        ...this.data.queryParam,
        freeEndTime: '',
      },
      endDate: '',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { publishSide } = options;
    this.setData({
      publishSide: publishSide
    })
    this.searchOrderCenterList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { pageSize, pageNum, total } = this.data.pagination;
    if (pageSize * pageNum >= total) {
      this.showErrorMessage("已加载全部数据");
    } else {
      this.setData({
        pagination: {
          ...this.data.pagination,
          pageNum: pageNum + 1
        }
      });
      this.searchOrderCenterList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/homev2/index'
    }
  }
})